//
//  MyViewController.m
//  BabyBedtimeStory
//
//  Created by qianfeng on 15-6-19.
//  Copyright (c) 2015年 张鹏毅. All rights reserved.
//

#import "MyViewController.h"
#import "localViewController.h"
#import "UIImageView+WebCache.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
}

-(void)creatTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSArray *arr = @[@[@"清空下载"],@[@"清空播放历史"],@[@"清除缓存"],@[@"关于我们"]];
    self.dataArr = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, kScreenSize.width, kScreenSize.height-49-64-100)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArr[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataArr[indexPath.section][indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self creatAlter];
    }
    if (indexPath.section == 1) {
        [[DBManager sharedManager]deleteAllModelWithRecordType:kFavorite ];
        [self creatAlter:@"播放历史已被清空"];
        if (self.block) {
            NSMutableArray *arr = nil;
            self.block(arr);
        }
    }
    if (indexPath.section == 2) {
        NSUInteger imageCaheSize = [[SDImageCache sharedImageCache]getSize];
        NSString *myCahaePath =[NSHomeDirectory() stringByAppendingString:@"Library/Caches/MyCaches"];
        NSDirectoryEnumerator *enumator = [[NSFileManager defaultManager]enumeratorAtPath:myCahaePath];
        __block NSUInteger count= 0;
        for (NSString *fileName in enumator) {
            NSString *path = [myCahaePath stringByAppendingString:fileName];
            NSDictionary *fileDict = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:nil];
            count += fileDict.fileSize;
        }
        CGFloat  totalaSize = ((CGFloat)imageCaheSize + count)/1024/1024;
        UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.5fM缓存",totalaSize] preferredStyle:UIAlertControllerStyleActionSheet];
        [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            [[SDImageCache sharedImageCache] clearDisk];
            [[SDImageCache sharedImageCache] clearMemory];
            NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
            [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
            
        }]];
        [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:sheet animated:YES completion:nil];
    }
}

-(void)isRingWithDataArr:(MyBlock)block{
    self.block = block;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)creatAlter{
    NSString *myCahaePath =[NSHomeDirectory() stringByAppendingString:@"/Documents/"];
    NSDirectoryEnumerator *enumator = [[NSFileManager defaultManager]enumeratorAtPath:myCahaePath];
    __block NSUInteger count= 0;
    for (NSString *fileName in enumator) {
        NSString *path = [myCahaePath stringByAppendingString:fileName];
        NSDictionary *fileDict = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:nil];
        count += fileDict.fileSize;
    }
    CGFloat  totalaSize = count/1024.0/1024.0;
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除本地数据" message:[NSString stringWithFormat:@"总共有%.5fM数据",totalaSize] preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/"];
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:sheet animated:YES completion:nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 3) {
        return 20;
    }
//    }else if(section == 4){
//        return 20;
//    }
    return 0;
}

@end
